package com.ruoyi.web.controller.market.index;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.db.domain.AppProfitInfo;
import com.ruoyi.market.enums.WarnEnum;
import com.ruoyi.market.factory.WarnFactory;
import com.ruoyi.market.model.result.WarnInfoResult;
import com.ruoyi.member.service.IMemberExtInfoIndexService;
import com.ruoyi.member.service.IMemberInfoService;
import com.ruoyi.profit.service.IAppProfitInfoService;
import com.ruoyi.special.service.ISpecialCostomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;

@Controller
@RequestMapping("/index/data")
public class IndexController {

    @Autowired
    IMemberExtInfoIndexService memberExtInfoIndexService;
    @Autowired
    ISpecialCostomInfoService specialCostomInfoService;
    @Autowired
    IAppProfitInfoService appProfitInfoService;
    @Autowired
    private IMemberInfoService memberInfoService;

    /**
     * 首页相关信息
     */
    @PostMapping( "/sortZhima")
    @ResponseBody
    public AjaxResult sortZhima(Integer type)
    {
        List<Map<String, Object>> dataList = memberExtInfoIndexService.getDataSortZhimaInfo(type);

        JSONObject returnData = new JSONObject();
        returnData.put("dataList", dataList);

        return success(returnData);
    }

    /**
     * 首页相关信息
     */
    @PostMapping( "/Zhima")
    @ResponseBody
    public AjaxResult Zhima(Integer type)
    {
        List<Map<String, Object>> dataList = memberInfoService.getDataSortZhimaInfo(type);

        JSONObject returnData = new JSONObject();
        returnData.put("dataList", dataList);

        return success(returnData);
    }

    /**
     * 首页相关信息
     */
    @PostMapping( "/organizeConsume")
    @ResponseBody
    public AjaxResult organizeConsume(Integer type)
    {
        List<Map<String, Object>> dataList = specialCostomInfoService.getDataSortOrganizeConsume(type);

        JSONObject returnData = new JSONObject();
        returnData.put("dataList", dataList);

        return success(returnData);
    }

    /**
     * 机构api
     */
    @PostMapping( "/organizeApi")
    @ResponseBody
    public AjaxResult organizeApi(Integer type)
    {
        // api 推送的数据
        List<Map<String, Object>> dataList = specialCostomInfoService.getDataSortOrganizeApi(type);

        JSONObject returnData = new JSONObject();
        returnData.put("dataList", dataList);

        return success(returnData);
    }

    /**
     * 首页相关信息
     */
    @PostMapping( "/warnInfo")
    @ResponseBody
    public AjaxResult warnInfo()
    {
        List<WarnInfoResult> dataList = new ArrayList<>();

        dataList.add(WarnFactory.getService(WarnEnum.SMSNOTICE.getValue()).handleWarnInfo(null));
        dataList.add(WarnFactory.getService(WarnEnum.SMSVERIFY.getValue()).handleWarnInfo(null));

        dataList.add(WarnFactory.getService(WarnEnum.ALI_ACCOUNT_BALANCE.getValue()).handleWarnInfo(null));

        JSONObject returnData = new JSONObject();
        returnData.put("dataList", dataList);

        return success(returnData);
    }


    /**
     * 首页相关信息
     */
    @PostMapping( "/profitInfo")
    @ResponseBody
    public AjaxResult profitInfo()
    {
        // api 推送的数据
        List<AppProfitInfo> resultList = appProfitInfoService.getIndexShowData();

        // 日收益
        List<JSONArray> dataList = new ArrayList<>();
        resultList.stream().forEach(appProfitInfo -> {

            JSONArray jsonArray = new JSONArray();
            jsonArray.add(appProfitInfo.getDateInfo());
            jsonArray.add(appProfitInfo.getDcProfit());
            jsonArray.add(appProfitInfo.getZdProfit());
            jsonArray.add(appProfitInfo.getTotalProfit());

            dataList.add(0, jsonArray);
        });

        JSONArray jsonArray = new JSONArray();
        jsonArray.add("日收益");
        jsonArray.add("贷超");
        jsonArray.add("助贷");
        jsonArray.add("总收益");

        dataList.add(0, jsonArray);

        // 月收益
        // api 推送的数据
        List<Map<String, Object>> monthResultList = appProfitInfoService.getIndexMonthShowData();
        // 日收益
        List<JSONArray> monthDataList = new ArrayList<>();
        for (Map<String, Object> appProfitInfo : monthResultList) {
            JSONArray jsonArray1 = new JSONArray();
            jsonArray1.add(appProfitInfo.get("monthInfo"));
            jsonArray1.add(appProfitInfo.get("dcProfit"));
            jsonArray1.add(appProfitInfo.get("zdProfit"));
            jsonArray1.add(appProfitInfo.get("totalProfit"));

            monthDataList.add(0, jsonArray1);
        }

        JSONArray jsonArray2 = new JSONArray();
        jsonArray2.add("月收益");
        jsonArray2.add("贷超");
        jsonArray2.add("助贷");
        jsonArray2.add("总收益");

        monthDataList.add(0, jsonArray2);

        JSONObject returnData = new JSONObject();
        returnData.put("dateDataList", dataList);
        returnData.put("monthDataList", monthDataList);

        return success(returnData);
    }

}
